package www.way2inbox.settings;

import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;



import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import www.way2inbox.settings.ApplicationConstants;
import com.way2sms.way2inbox.R;

/**
 * 
 * @author Swetha Bejugam
 * 
 */
public class SettingsActivity extends PreferenceActivity implements
		UncaughtExceptionHandler {
	TelephonyManager tlpnmgr;
	ImageView imgCancel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {

			this.requestWindowFeature(Window.FEATURE_NO_TITLE);
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_settings);
			((TextView) findViewById(R.id.title)).setText("Settings");

			@SuppressWarnings("unused")
			UncaughtExceptionHandler mDfltExceptionHandler = Thread
					.getDefaultUncaughtExceptionHandler();
			Thread.setDefaultUncaughtExceptionHandler(this);
			/*
			 * To get values from settings xml preference keys and send key to
			 * below method
			 */
			getPrefKeyValues(ApplicationConstants.SettingsPreferenceKeys.GEN_PLAY_SENT_SOUND);
			/* set values to preference text */
			setValuetoPrefKey();
			/* To get values entered in edittext preferences */
			getEdittextPrefValues(ApplicationConstants.SettingsPreferenceKeys.GEN_SMS_COST);

		} catch (Exception e) {
			System.err.println(e);
		}
	}

	private String getEdittextPrefValues(String edtPrefKey) {
		Preference edit1 = findPreference(edtPrefKey);

		// System.out.println(">>>Editvalue>>"
		// + String.valueOf(((EditTextPreference) edit1).getText()
		// .toString()));
		return String.valueOf(((EditTextPreference) edit1).getText().toString());
	}

	private void setValuetoPrefKey() {
		/* set values to preference text */
		getPreferenceScreen().findPreference(
				ApplicationConstants.SettingsPreferenceKeys.GEN_ABOUT)
				.setSummary(android.os.Build.VERSION.RELEASE);
		tlpnmgr = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		getPreferenceScreen().findPreference(
				ApplicationConstants.SettingsPreferenceKeys.MMS_GROUPCHAT)
				.setSummary(
						ApplicationConstants.USE_MMS_GROUPCHAT_SUB
								+ tlpnmgr.getLine1Number());

	}

	private boolean getPrefKeyValues(String key) {
		/* To get values from settings xml preference keys for booelans */
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		return prefs.getBoolean(key, false);
	}

	@SuppressWarnings("unused")
	private void alarm() {
		SharedPreferences getAlarms = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		String alarms = getAlarms.getString("SMS_RINGTONE", "default ringtone");
		Uri uri = Uri.parse(alarms);
		playSound(this, uri);

		// call mMediaPlayer.stop(); when you want the sound to stop
	}

	private MediaPlayer mMediaPlayer;

	private void playSound(Context context, Uri alert) {
		mMediaPlayer = new MediaPlayer();
		try {
			mMediaPlayer.setDataSource(context, alert);
			final AudioManager audioManager = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			}
		} catch (IOException e) {
			
			System.out.println("OOPS");
		}
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		System.err.println(ex);
	}
}
